package com.example.lenovo.demo.presenter;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.example.lenovo.demo.CityActivity;
import com.example.lenovo.demo.MapActivity;
import com.example.lenovo.demo.MyApplication;
import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.Model;
import com.example.lenovo.demo.model.databean.CityBean;
import com.example.lenovo.demo.model.databean.ModelDaysBean;
import com.example.lenovo.demo.model.idatabean.IModel;
import com.example.lenovo.demo.model.idatabean.OnCityClickSuccess;
import com.example.lenovo.demo.model.idatabean.OnModelDaysSuccess;
import com.example.lenovo.demo.presenter.adapter.CityActivityLvAdapter;
import com.example.lenovo.demo.view.ifgview.ICityActivity;
import com.example.lenovo.demo.view.utils.GvHeightUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2017/1/13.
 */

public class CityActivityPresenter implements ICityActivity{
    private MapView mapView;
    private TextView msg1,msg2;
    private ListView listView;
    private Bitmap bitmap;
    private LatLng latLng;
    private BitmapDescriptor bitmaps ;
    private String id;
    private List<OverlayOptions> optionses;
    private CityActivityLvAdapter adapter;
    private List<ModelDaysBean.DataBean.DaysBean> data2;
    private LayoutInflater inflater;
    private BaiduMap baiduMap;
    private Context context;
    private IModel imodel;
    private ProgressDialog progressDialog;

    public void init(){
        progressDialog.show();
        imodel=new Model();
        imodel.sendModelDaysBean(id, new OnModelDaysSuccess() {
            @Override
            public void ModelDaysSuccess(final ModelDaysBean modelDaysBean) {
                int plans=0;
                data2.addAll(modelDaysBean.getData().getDays());
                for (ModelDaysBean.DataBean.DaysBean daysBean : data2) {
                    List<ModelDaysBean.DataBean.DaysBean.PointsBean> poins=daysBean.getPoints();
                    for (ModelDaysBean.DataBean.DaysBean.PointsBean poin : poins) {
                        latLng=new LatLng(poin.getPoi().getLat(),poin.getPoi().getLng());
                        OverlayOptions option = new MarkerOptions()
                                .position(latLng)
                                .icon(bitmaps);
                        optionses.add(option);
                        plans++;
                    }
                }
                msg2.setText(data2.size()+"天,"+plans+"条旅行计划");
                baiduMap.addOverlays(optionses);
                initMainLocation(baiduMap,modelDaysBean.getData().getDestination().getLat(),modelDaysBean.getData().getDestination().getLng());

                /**
                 * ------
                 * */

                adapter=new CityActivityLvAdapter(modelDaysBean.getData().getDays(), context, modelDaysBean.getData().getDestination().getName(), new OnCityClickSuccess() {
                    @Override
                    public void CitySuccess(int i) {
                        Intent intent=new Intent(context,MapActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putInt("num",i);
                        bundle.putParcelable("msg",modelDaysBean);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }
                });
                listView.setAdapter(adapter);
                GvHeightUtil.setListViewHeightBasedOnChildren(listView,0);
                progressDialog.dismiss();

            }

            @Override
            public void ModelDaysFailed(List<ModelDaysBean.DataBean> data) {

            }
        });
        initView();
        initData();


    }

    private void initData() {

        UiSettings settings=baiduMap.getUiSettings();
        settings.setAllGesturesEnabled(false);   //关闭一切手势操作
        settings.setOverlookingGesturesEnabled(false);//屏蔽双指下拉时变成3D地图
        settings.setZoomGesturesEnabled(false);//获取是否允许缩放手势返回:是否允许缩放手势



        optionses=new ArrayList<>();
        data2=new ArrayList<>();


    }

    private void initView() {
        initBitMap();
        inflater=LayoutInflater.from(context);
        View v=inflater.inflate(R.layout.cityactivity_lvheader,null,false);
        mapView= (MapView) v.findViewById(R.id.activity_city_mapview);
        msg1= (TextView) v.findViewById(R.id.activity_city_msg1);
        msg2= (TextView) v.findViewById(R.id.activity_city_msg2);
        msg1.setText("行程地图");

        listView.addHeaderView(v);
        baiduMap=mapView.getMap();

    }



    private void initBitMap() {
        bitmap= BitmapFactory.decodeResource(context.getResources(), R.drawable.location);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        // 设置想要的大小
        int newWidth = 40;
        int newHeight = 40;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
                true);
        bitmaps = BitmapDescriptorFactory.fromBitmap(bitmap);
    }

    private void initMainLocation(BaiduMap baiduMap,double lat, double lng) {
        LatLng cenpt = new LatLng(lat,lng);
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(12)
                .build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        baiduMap.setMapStatus(mMapStatusUpdate);
    }


    @Override
    public void sendView(Context context, ListView listView, String id,ProgressDialog progressDialog) {
        this.context=context;
        this.id=id;
        this.listView=listView;
        this.progressDialog=progressDialog;
    }
}
